package edu.uwlax.cs.oayonlinestore.client.gui.shop;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.Order;
import edu.uwlax.cs.oayonlinestore.client.gui.Store;
import edu.uwlax.cs.oayonlinestore.client.gui.shared.OrderSummary;
import edu.uwlax.cs.oayonlinestore.client.gui.shared.Const.OrderStatus;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.HSpacer;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.OrderEditVO;

public class PageConfirm extends TitlePage {

	private class ClickHandler extends DefaultAsyncCallback implements
			ClickListener {
		public void onClick(Widget arg0) {
			OnlinestoreRService.Util.getInstance().placeOrder(
					Order.get().getId(), this);
		}

		public void onSuccess(Object arg0) {
			History.newItem(Store.getFrame().getShopFrame().getReciept()
					.getAnchor(Order.get().getId()));
			Order.get().update();
		}
	}

	private OrderSummary sum;

	private Button confBtn;

	protected void create() {
		setTitle("Confirm order");
		add(sum = new OrderSummary() {
			protected void setOrderEdit(OrderEditVO oe) {
				super.setOrderEdit(oe);
				confBtn.setEnabled(OrderStatus.IN_PROGRESS.equals(oe
						.getStatus()));
			}
		});
		add(new HSpacer());

		add(confBtn = new Button("Confirm and Place Order", new ClickHandler()));
	}

	public String getID() {
		return "Confirm";
	}

	protected void onShow() {
		sum.update(Order.get().getId());
	}

}
